import br.com.sg5.flx.crypto.MD5;
import br.com.sg5.flx.event.LoginEvent;

import flash.events.Event;
import flash.events.EventDispatcher;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
import mx.rpc.AsyncToken;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;

import org.dphibernate.events.LazyLoadEvent;
import org.dphibernate.rpc.HibernateManaged;
import org.dphibernate.rpc.HibernateRemoteObject;
import org.dphibernate.util.PropertyHelper;

private var UsuarioService:RemoteObject=new RemoteObject("usuarioService");


[Bindable]
private var usuario:ArrayCollection =new ArrayCollection;

[Bindable]
public function logar():void
{
	UsuarioService.showBusyCursor = true;					
	UsuarioService.addEventListener(ResultEvent.RESULT, fncOk);
	UsuarioService.addEventListener(FaultEvent.FAULT, fncFalha);
	
	//MD5.hash
	var hash:String = MD5.hash(txsenha.text);
	UsuarioService.getLogin(txusuario.text,txsenha.text)							 
}	 

private function fncOk(e:ResultEvent):void
{
	if(!e.result)
	{
		status.text = 'Login ou senha inválido!';
		status.includeInLayout = true;
	}
	else
	{
		status.text = '';
		status.includeInLayout = false; 
		this.currentState = "Logado";
		this.swd.text = swd.text +" "+txusuario.text;
		
	}
}
private function ok():void{
	
	dispatchEvent(new LoginEvent(LoginEvent.LOGADO))	
}

private function fncFalha(e:FaultEvent):void
{
	status.text = 'Login ou senha inválido!';
	status.includeInLayout = true
}